<?php

namespace App\Console\Commands;

use App\Models\Game\Center\AccountDayStat;
use App\Models\Game\Center\Action;
use App\Models\Game\Center\CplActivityIssue;
use App\Models\Game\Center\Deliver;
use App\Models\Game\Center\PayChannelStat;
use App\Models\Game\Center\PayOrder;
use App\Models\Game\Center\SmsCodeLog;
use App\Models\Game\Center\SourceLtv;
use App\Models\Game\Center\SourceLtvDetail;
use App\Models\Game\Center\WithdrawOrder;
use App\Models\Game\Main\Mail;
use App\Models\Game\Main\MailGlobal;
use App\Models\ManagerLog;
use App\Models\Mongo\DailyLog\BehaviorLog;
use App\Models\Mongo\DailyLog\ClientCatchLog;
use App\Models\Mongo\DailyLog\ClientLog;
use App\Models\Mongo\DailyLog\OnlineStat;
use App\Models\Mongo\DailyLog\PayCntHourStat;
use App\Models\Mongo\PlayerCache\CurrencyDayStatistics;
use App\Models\Mongo\PlayerCache\GoodsModifyRecord;
use App\Models\Mongo\PlayerCache\GoodsModifyStatistics;
use App\Models\Mongo\PlayerCache\GoodsModifyStatisticsAll;
use App\Models\Mongo\PlayerCache\GoodsModifyStatisticsItem;
use App\Models\Mongo\PlayerCache\SceneOnline;
use App\Models\Mongo\PlayerCache\SceneProfit;
use App\Models\Mongo\PlayerCache\SceneRunningWater;
use App\Models\Mongo\PlayerCache\StockProfit;
use App\Models\PersonalAccessToken;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CleanExpired extends Command
{
    protected $signature = 'clean:expire';

    protected $description = '清除过期数据';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $this->dbAdmin();
        $this->dbCenter();
        $this->dbGame001();
        $this->dbDailyLog();
        $this->dbPlayerCache();
    }

    private function dbAdmin()
    {
        ManagerLog::where('created_at', '<', now()->subMonth())->delete();
        PersonalAccessToken::where('last_used_at', '<', now()->subDays(45)->startOfDay())->delete();
        DB::table('laravel_sms_log')->where('created_at', '<', now()->subMonth()->startOfDay())->delete();
    }

    private function dbCenter()
    {
        AccountDayStat::where('day', '<', now()->subMonths(3)->toDateString())->delete();
        CplActivityIssue::where('end_at', '<', now()->subDay())->get()->each(function (CplActivityIssue $cplActivityIssue) {
            $cplActivityIssue->status = CplActivityIssue::STATUS_OFF;
            $cplActivityIssue->save();
        });
        SmsCodeLog::where('expired_at', '<', now()->subMonth()->startOfDay())->delete();

        // PayOrder::where('updated_at', '<', now()->subMonths(3)->startOfDay())->delete();
        // WithdrawOrder::where('updated_at', '<', now()->subMonths(3)->startOfDay())->delete();

        PayOrder::where('updated_at', '<', now()->subMonth()->startOfDay())->where('status', PayOrder::STATUS_WAIT)->delete();
        WithdrawOrder::where('updated_at', '<', now()->subMonth())->where('status', WithdrawOrder::STATUS_FAILED)->delete();

        SourceLtv::where('day', '<', now()->subMonths(3)->toDateString())->delete();
        SourceLtvDetail::where('dataDay', '<', now()->subMonths(3)->toDateString())->delete();

        Action::where('day', '<', (int) now()->subMonths(3)->format('Ymd'))->delete();
        Deliver::where('created_at', '<', now()->subMonths(3)->startOfDay())->delete();
        PayChannelStat::where('day', '<', now()->subMonths(3)->toDateString())->delete();
    }

    private function dbGame001()
    {
        Mail::where('updated_at', '<', now()->subMonths(3)->startOfDay())->delete();
        MailGlobal::where('expired_at', '<', now()->toDateTimeString())->where('status', MailGlobal::STATUS_ON)->update([
            'status' => MailGlobal::STATUS_EXPIRED,
        ]);
    }

    private function dbDailyLog()
    {
        OnlineStat::where('day', '<', (int) now()->subMonths(3)->format('Ymd'))->delete();
        // DayStat::where('day', '<', now()->subMonths(3)->toDateString())->delete();
        // MonthStat::where('month', '<', now()->subMonths(3)->format('Y-m'))->delete();
        // HourStat::where('day', '<', now()->subMonths(3)->toDateString())->delete();
        // PayCntHourStat::where('day', '<', now()->subMonths(3)->toDateString())->delete();
        // DayCpaCost::where('day', '<', now()->subMonths(3)->toDateString())->delete();

        ClientLog::where('created_at', '<', now()->subMonths(3)->startOfDay())->delete();
        ClientCatchLog::where('created_at', '<=', now()->subMonths(3))->delete();
        BehaviorLog::where('created_at', '<', now()->subMonth()->startOfDay())->delete();
    }

    private function dbPlayerCache()
    {
        CurrencyDayStatistics::where('create_at', '<', now()->subMonths(3)->startOfDay())->delete();
        SceneOnline::where('create_at', '<', now()->subMonth())->delete();
        SceneProfit::where('create_at', '<', now()->subMonths(3))->delete();
        StockProfit::where('day', '<', now()->subMonths(3)->toDateString())->delete();
        SceneRunningWater::where('day', '<', (int) now()->subMonths(3)->format('Ymd'))->delete();

        GoodsModifyStatistics::where('create_at', '<', now()->subMonths(3))->delete();
        GoodsModifyStatisticsAll::where('create_at', '<', now()->subMonths(3))->delete();
        GoodsModifyStatisticsItem::where('create_at', '<', now()->subMonths(3))->delete();
    }
}
